const onSubmit = async () => {
  try {
    if (!signCanvas.value) {
      ElMessage.warning('请先签名');
      return;
    }
    
    // 检查签名是否为空
    const imageData = ctx?.getImageData(0, 0, signCanvas.value.width, signCanvas.value.height).data;
    if (!imageData || imageData.every(pixel => pixel === 0 || pixel === 255)) {
      ElMessage.warning('签名不能为空，请签名后再提交');
      return;
    }
    
    // 1. 生成Base64字符串
    const signatureDataUrl = signCanvas.value.toDataURL('image/png');
    
    // 2. 生成图片文件名
    const imageName = await generateImageName(signatureDataUrl);
    
    // 3. 更新表单中的签名URL和视觉听力信息
    form.value.signUrl = imageName;
    form.value.vision = '左眼：' + form.value.lefteye + ' 右眼：' + form.value.righteye;
    form.value.hearing = '左耳：' + form.value.leftear + ' 右耳：' + form.value.rightear;

    // 4. 准备上传数据
    const formData = new FormData();
    const blob = dataURLtoBlob(signatureDataUrl);
    formData.append('file', blob, imageName);
    
    // 5. 先上传签名图片
    const uploadResult = await http.post('/api/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
    
    if (!uploadResult || uploadResult.code !== 0) {
      throw new Error('签名图片上传失败');
    }
    
    // 6. 提交表单数据
    const result = await http.post('/api/InAssess/Add', form.value);
    
    if (result && (result.code === 0 || result.success)) {
      ElMessage.success('入院评估单保存成功');
      router.push('/InAssessButton');
    } else {
      throw new Error(result?.message || '保存失败');
    }
  } catch (error) {
    console.error('上传出错:', error);
    ElMessage.error('上传过程中出错: ' + (error.message || '未知错误'));
  }
} 